/*
 * Class NPC
 *
 * Version 1.0
 *
 * Monday, August 18, 2008
 *
 * Created by Palidino76
 */

package palidino76.rs2.npcs;

public class NPC {
    /**
     * The position this NPC is stored in the npc array in the Engine class.
     */
    public int npcId = -1;
    /**
     * The NPC's id, such as 50 for king black dragon.
     */
    public int npcType = 0;
    /**
     * The name of this NPC.
     */
    public String name = "";
    /**
     * If it should hit 0 HP, how long it should take to respawn.
     */
    public int respawnDelay = 60;
    /**
     * Its combat level, used for calculating its accuracy and defence.
     */
    public int combatLevel = 0;
    /**
     * Max hitpoints it can have.
     */
    public int maxHP = 0;
    /**
     * The basic max hit it can have, with an attack method you  can get more specific.
     */
    public int maxHit = 0;
    /**
     * Attack type, 0 for melee, 1 for range, 2 for magic.
     */
    public int atkType = 0;
    /**
     * The weakness, same setup as atkType.
     */
    public int weakness = 0;
    /**
     * The current HP this NPC has.
     */
    public int currentHP = 0;
    /**
     * The direction this NPC is moving in.
     */
    public int direction = -1;
    /**
     * The height level its at.
     */
    public int heightLevel = 0;
    /**
     * Request for updating facing.
     */
    public boolean faceToUpdateReq = false;
    public int faceToRequest = -1;
    /**
     * If the NPC is dead or not.
     */
    public boolean isDead = false;
    /**
     * If the death emote has been requested, move on to the next part of the death process.
     */
    public boolean deadEmoteDone = false;
    /**
     * Hide the NPC until it is ready to respawn.
     */
    public boolean hiddenNPC = false;
    /**
     * Absolute positioning.
     */
    public int absX = 0, absY = 0;
    /**
     * If false, the NPC will not respawn if it dies.
     */
    public boolean needsRespawn = false;
    /**
     * If set to true, this NPC will randomly walk around.
     */
    public boolean randomWalk = true;
    /**
     * If an update is needed.
     */
    public boolean updateReq = false;
    /**
     * If the NPC should speak.
     */
    public boolean speakTextUpdateReq = false;
    public String speakText = "";
    /**
     * Hit requests.
     */
    public boolean hit1UpdateReq = false;
    public boolean hit2UpdateReq = false;
    public int hitDiff1 = 0;
    public int posionHit1 = 0;
    public int hitDiff2 = 0;
    public int posionHit2 = 0;
    /**
     * Animation request.
     */
    public boolean animUpdateReq = false;
    public int animRequest = 65535;
    public int animDelay = 0;
    /**
     * Graphic request.
     */
    public boolean gfxUpdateReq = false;
    public int gfxRequest = 65535;
    public int gfxDelay = 0;
    /**
     * Facing request.
     */
    public boolean faceCoordsUpdateReq = false;
    public int faceCoordsX = -1;
    public int faceCoordsY = -1;
    /**
     * The area this NPC can randomly walk around.
     */
    public int moveRangeX1 = 0;
    public int moveRangeY1 = 0;
    public int moveRangeX2 = 0;
    public int moveRangeY2 = 0;
    /**
     * What should be added onto the absolute positioning if this NPC moves.
     */
    public int moveX = 0;
    public int moveY = 0;
    /**
     * The original position the NPC spawned at.
     */
    public int makeX = 0;
    public int makeY = 0;
    /**
     * Delay before the NPC can attack again.
     */
    public int combatDelay = 0;

    /**
     * Constructs a new NPC class.
     * @param type The type of NPC.
     * @param index The position the NPC is at.
     */
    public NPC(int type, int index) {
        npcType = type;
        npcId = index;
    }

    /**
     * This method is called every 600 milliseconds.
     */
    public void process() {
        if (respawnDelay > 0 && isDead) {
            respawnDelay--;
        }
        if (combatDelay > 0) {
            combatDelay--;
        }
    }

    /**
     * Request an animation for this NPC.
     * @param animId The amination to perform.
     * @param animD The delay before doing the animation.
     */
    public void requestAnim(int animId, int animD) {
        animRequest = animId;
        animDelay = animD;
        animUpdateReq = true;
        updateReq = true;
    }

    /**
     * Request text for this NPC.
     * @param message The message to make the NPC say.
     */
    public void requestText(String message) {
        speakText = message;
        animUpdateReq = true;
        speakTextUpdateReq = true;
    }

    /**
     * Request an graphic for this NPC.
     * @param gfxId The graphic to perform.
     * @param gfxD The delay or height or the gfx depending on the value.
     */
    public void requestGFX(int gfxId, int gfxD) {
        if (gfxD >= 100) {
            gfxD += 6553500;
        }
        gfxRequest = gfxId;
        gfxDelay = gfxD;
        gfxUpdateReq = true;
        updateReq = true;
    }

    /**
     * Request this NPC faces two coordinates.
     * @param x The x coordinate to face.
     * @param y The y coordinate to face.
     */
    public void requestFaceCoords(int x, int y) {
        faceCoordsX = 2 * x + 1;
        faceCoordsY = 2 * y + 1;
        faceCoordsUpdateReq = true;
        updateReq = true;
    }

    /**
     * Request this NPC faces another NPC or player.
     * @param faceId The target to face.
     */
    public void requestFaceTo(int faceId) {
        faceToRequest = faceId;
        faceToUpdateReq = true;
        updateReq = true;
    }

    /**
     * Add damage to this NPC.
     * @param damage To amount of damage.
     * @param posion 0 for normal damage, 1 for posion.
     */
    public void appendHit(int damage, int posion) {
        if (damage > currentHP) {
            damage = currentHP;
        }
        currentHP -= damage;
        if (currentHP <= 0) {
            currentHP = 0;
            isDead = true;
        }
        if (!hit1UpdateReq) {
            hitDiff1 = damage;
            posionHit1 = posion;
            hit1UpdateReq = true;
        } else {
            hitDiff2 = damage;
            posionHit2 = posion;
            hit2UpdateReq = true;
        }
        updateReq = true;
    }
}